package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.CertificateDto;
import com.jyy.prefabricated.dto.ExamCertificateDto;
import com.jyy.prefabricated.entity.Certificate;
import com.jyy.prefabricated.entity.ExamCertificate;
import com.jyy.prefabricated.entity.ScoringComments;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.CertificateMapper;
import com.jyy.prefabricated.mapper.ExamCertificateMapper;
import com.jyy.prefabricated.mapper.ScoringCommentsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class CertificateService {

    @Resource
    private CertificateMapper certificateMapper;
    @Resource
    private ScoringCommentsMapper scoringCommentsMapper;
    @Resource
    private ExamCertificateMapper examCertificateDao;
    /**
     * 新增
     * @param certificateDto
     * @return
     */
    public Integer insert(CertificateDto certificateDto){
        Certificate certificate = new Certificate();
        BeanUtils.copyProperties(certificateDto,certificate);
        certificateMapper.insert(certificate);
        if(certificateDto.getScoringComments() != null && certificateDto.getScoringComments().size()>0){
            for (ScoringComments scoringComment:certificateDto.getScoringComments()) {
                scoringComment.setCfId(certificate.getId());
            }
            scoringCommentsMapper.insertBatch(certificateDto.getScoringComments());
        }
        List<ExamCertificate> examCertificates = new ArrayList<>();
        if(certificateDto.getGrantRules() != null && certificateDto.getGrantRules().size()>0){
            for (ExamCertificateDto examCertificateDto:certificateDto.getGrantRules()) {
                ExamCertificate examCertificate = new ExamCertificate();
                BeanUtils.copyProperties(examCertificateDto,examCertificate);
                examCertificate.setCfId(certificate.getId());
                examCertificates.add(examCertificate);
            }
            examCertificateDao.insertBatch(examCertificates);
        }
        return certificate.getId();
    }

    /**
     * 更新
     * @param certificateDto
     * @return
     */
    public int update(CertificateDto certificateDto){
        if(certificateDto.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        Certificate certificate = new Certificate();
        BeanUtils.copyProperties(certificateDto,certificate);
        scoringCommentsMapper.deleteByCfId(certificate.getId());
        examCertificateDao.deleteByCfId(certificate.getId());
        if(certificateDto.getScoringComments() != null && certificateDto.getScoringComments().size()>0){
            for (ScoringComments scoringComment:certificateDto.getScoringComments()) {
                scoringComment.setCfId(certificate.getId());
            }
            scoringCommentsMapper.insertBatch(certificateDto.getScoringComments());
        }
        List<ExamCertificate> examCertificates = new ArrayList<>();
        if(certificateDto.getGrantRules() != null && certificateDto.getGrantRules().size()>0){
            for (ExamCertificateDto examCertificateDto:certificateDto.getGrantRules()) {
                ExamCertificate examCertificate = new ExamCertificate();
                BeanUtils.copyProperties(examCertificateDto,examCertificate);
                examCertificate.setCfId(certificate.getId());
                examCertificates.add(examCertificate);
            }
            examCertificateDao.insertBatch(examCertificates);
        }
        return certificateMapper.updateByPrimaryKeySelective(certificate);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        Certificate certificate = certificateMapper.selectByPrimaryKey(id);
        certificate.setDeleted(true);
        return certificateMapper.updateByPrimaryKeySelective(certificate);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public CertificateDto selectById(Integer id){
        Certificate certificate = certificateMapper.selectByPrimaryKey(id);
        CertificateDto certificateDto = new CertificateDto();
        BeanUtils.copyProperties(certificate,certificateDto);
        ScoringComments scoringComment = new ScoringComments();
        scoringComment.setCfId(id);
        List<ScoringComments> scoringComments = scoringCommentsMapper.queryAll(scoringComment);
        certificateDto.setScoringComments(scoringComments);
        ExamCertificate examCertificate = new ExamCertificate();
        examCertificate.setCfId(id);
        List<ExamCertificateDto> examCertificateDtos =  examCertificateDao.queryExams(examCertificate);
        certificateDto.setGrantRules(examCertificateDtos);
        return certificateDto;
    }

    /**
     * 查询列表
     * @return
     */
    public List<Certificate> selectAll(){
        return certificateMapper.selectAll();
    }
}
